var tbList
var removeUser;
layui.use(['table','form','layer','jquery'], function(){
    var table = layui.table;
    var $ = layui.$;
  var form = layui.form;
  var layer = parent.layer ? parent.layer : layui.layer;
   
   removeUser=function(id){
    	layer.confirm('确定要删除吗？', {
			  btn: ['确定','取消'] //按钮
			}, function(index){
				  $.ajax({
		    		type:"delete",
		    		url:"http://localhost:8080/api/devusers/"+id,
		    		success:function(result){
						layer.close(index);
		    			tbList.reload();
						
		    		}
	    		  });
			});
    	
    }
   
  // 添加
  $('.btnAdd').click(function () {
    layer.open({
      type: 2, 
      area: ['550px', '400px'],
      content: '/layuicms2.0/page/devuser/add.html' 
    });
  })
  
  
    

    //第一个实例
    tbList=table.render({
      elem: '#userList'
      ,height: 312
      ,url: 'http://localhost:8080/api/devusers' //数据接口
      ,page: true //开启分页
      ,cols: [[ //表头
        {field: 'Id', title: 'ID', width:80, sort: true, fixed: 'left'}
        ,{field: 'Name', title: '用户名', width:80}
        ,{field: 'Email', title: '邮箱'}
        ,{field: 'State', title: '状态', templet:'#changeState', width:80} 
        ,{title:'操作' ,templet:'#opt'}
        ]]
        , limit: 8
        , limits: [5, 8, 10, 20]
        , request: {
            pageName: 'pageIndex' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where: {
            name: $('.searchVal').val()
        }
    });
    
    
    //改变状态
    form.on('switch(state)', function (obj) {
   
    // 获取userId
    var userId = $(this).attr('userId');
    console.log(userId);
    $.ajax({
      type: "get",
      // url: "http://localhost:8080/api/DevUsers/changestate/"+userId,
      url: "http://localhost:8080/api/DevUsers/efchangestate/" + userId,
      data:{state:obj.elem.checked?1:0},
      success: function (response) {
        console.log('设置完成！');
        console.log(layer);
        layer.msg('设置完成！');
      }
    });
    })
  });